package com.lingo.mgr.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.lingo.common.utils.StringUtils;
import com.lingo.common.core.page.TableDataInfo;
import com.lingo.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.lingo.mgr.domain.bo.GptApiKeyConfigBo;
import com.lingo.mgr.domain.vo.GptApiKeyConfigVo;
import com.lingo.mgr.domain.GptApiKeyConfig;
import com.lingo.mgr.mapper.GptApiKeyConfigMapper;
import com.lingo.mgr.service.IGptApiKeyConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * chatgpt的api-key配置Service业务层处理
 *
 * @author lingo
 * @date 2023-11-22
 */
@RequiredArgsConstructor
@Service
public class GptApiKeyConfigServiceImpl implements IGptApiKeyConfigService {

    private final GptApiKeyConfigMapper baseMapper;

    /**
     * 查询chatgpt的api-key配置
     */
    @Override
    public GptApiKeyConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询chatgpt的api-key配置列表
     */
    @Override
    public TableDataInfo<GptApiKeyConfigVo> queryPageList(GptApiKeyConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GptApiKeyConfig> lqw = buildQueryWrapper(bo);
        Page<GptApiKeyConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询chatgpt的api-key配置列表
     */
    @Override
    public List<GptApiKeyConfigVo> queryList(GptApiKeyConfigBo bo) {
        LambdaQueryWrapper<GptApiKeyConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GptApiKeyConfig> buildQueryWrapper(GptApiKeyConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GptApiKeyConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getApiKey()), GptApiKeyConfig::getApiKey, bo.getApiKey());
        lqw.eq(bo.getStatus() != null, GptApiKeyConfig::getStatus, bo.getStatus());
        lqw.eq(bo.getLlmVersion() != null, GptApiKeyConfig::getLlmVersion, bo.getLlmVersion());
        return lqw;
    }

    /**
     * 新增chatgpt的api-key配置
     */
    @Override
    public Boolean insertByBo(GptApiKeyConfigBo bo) {
        GptApiKeyConfig add = BeanUtil.toBean(bo, GptApiKeyConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改chatgpt的api-key配置
     */
    @Override
    public Boolean updateByBo(GptApiKeyConfigBo bo) {
        GptApiKeyConfig update = BeanUtil.toBean(bo, GptApiKeyConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GptApiKeyConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除chatgpt的api-key配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
